﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace Srvtools
{
    public partial class UsasComboBoxText : ComboBox, ISupportInitialize
    {
        string m_FileName = "";
        UsasComboBoxTextForm m_frm = null;

        public void BeginInit()
        {

        }

        private bool _ShowTipMessage = true;
        [Category("USAS"), Description("是否启用提示文字"), DefaultValue(true)]
        public bool ShowTipMessage
        {
            get { return _ShowTipMessage; }
            set { _ShowTipMessage = value; }
        }

        private bool _EnterEnable = true;
        [Category("USAS"),
        Description("Indicate whether user can leave the control by pressing key of enter"), DefaultValue(true)]
        public bool EnterEnable
        {
            get { return _EnterEnable; }
            set { _EnterEnable = value; }
        }

        public void EndInit()
        {
            if (!this.DesignMode)
            {                
                string pathName = Application.StartupPath + "\\" + Srvtools.CliUtils.fCurrentProject;
                m_FileName = pathName + "\\" + this.FindForm().GetType().Namespace + "." + this.FindForm().Name + "." + this.Name + ".txt";               
                this.KeyDown += new KeyEventHandler(ComboBoxText_KeyDown);
               // this.FindForm().Text = m_FileName;
                LoadData();
            }
        }

        public void ShowInputComment()
        {

            System.Drawing.Rectangle rec = new System.Drawing.Rectangle(
                this.PointToScreen(new System.Drawing.Point(0, 0)), this.Size);
            m_frm = new UsasComboBoxTextForm(m_FileName, rec);
            m_frm.FormClosed += new FormClosedEventHandler(m_frm_FormClosed);
            m_frm.TopMost = true;
            m_frm.Show(this.FindForm());

        }
        
        void ComboBoxText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.T)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(
                    this.PointToScreen(new System.Drawing.Point(0, 0)), this.Size);               
                m_frm = new UsasComboBoxTextForm(m_FileName, rec);
                m_frm.FormClosed += new FormClosedEventHandler(m_frm_FormClosed);
                m_frm.TopMost = true;
                m_frm.Show(this.FindForm());              
            }
            if ((!e.Control && !e.Shift) && e.KeyCode == Keys.Enter)
            {
                if (this.EnterEnable)  SendKeys.Send("{Tab}");
            }
        }

        void m_frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadData();
        }
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
           if ( this.Text  == "<Ctrl+T 弹出编辑器>") 
            {
                this.SelectedIndex =-1;
                this.Text = "";
            }
        }

        void LoadData()
        {
            this.Items.Clear();
            string str = "";
            this.Items.Add(str);
            if (System.IO.File.Exists(m_FileName))
            {
                System.IO.StreamReader sr = new StreamReader(m_FileName);                
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Trim() != "")
                    {
                        this.Items.Add(str);
                    }
                }
                sr.Close();
            }

          if(_ShowTipMessage == true)  this.Items.Add("<Ctrl+T 弹出编辑器>");
        }
    }
}
